﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using Newtonsoft.Json;

namespace ATW.MT.SellManage
{
    public partial class SetOrder : System.Web.UI.Page
    {
        bus_T_sort sort = new bus_T_sort();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["handler"]) ||
                !string.IsNullOrEmpty(Request.Form["handler"]))
            {
                handleRequest();
                return;
            }
            BindData();
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]) && !string.IsNullOrEmpty(Request.QueryString["pname"]))
            {
                string pid = Request.QueryString["pid"];
                string pname = Request.QueryString["pname"];
                if (!common.filterChar(pid) || !common.filterChar(pname))
                {
                    return;
                }
                lblName.Text = pname;
                hdfpid.Value = pid;
            }
        }
        private void handleRequest()
        {//处理请求
            string handler = string.IsNullOrEmpty(Request.QueryString["handler"]) ? Request.Form["handler"] : Request.QueryString["handler"];
            try
            {
                string strRes = "";
                if (handler == "load")
                {
                    string pid = Request.QueryString["pid"];
                    string type = Request.QueryString["type"];
                    if (!string.IsNullOrEmpty(pid) && !string.IsNullOrEmpty(type)
                        && common.filterChar(pid) && common.filterChar(type))
                    {
                        if (type == "ft")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getFT(pid));
                        }
                        if (type == "fb")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getFB(pid));
                        }
                        if (type == "fg")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getFG(pid));
                        }
                        if (type == "fz")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getFZ(pid));
                        }
                        if (type == "tl")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getTL(pid));
                        }
                        if (type == "fj")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getFJ(pid));
                        }
                        if (type == "kj")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getKJ(pid));
                        }
                        if (type == "yl")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getYL(pid));
                        }
                        if (type == "sj")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getSJ(pid));
                        }
                        if (type == "lj")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getLJ(pid));
                        }
                        if (type == "jg")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getJG(pid));
                        }
                        if (type == "mf")
                        {
                            strRes = JsonConvert.SerializeObject(sort.getMF(pid));
                        }
                    }
                }
                if (handler == "set")
                {
                    bool flag = false;
                    string data = Request.Form["data"];
                    string type = Request.Form["type"];
                    if (!string.IsNullOrEmpty(data) && !string.IsNullOrEmpty(type)
                        && common.filterChar(data) && common.filterChar(type))
                    {
                        string[] list = data.Split(new char[] { '|' });
                        if (type == "ft")
                        {
                            flag = sort.setMaterial(list);
                        }
                        if (type == "fb")
                        {
                            flag = sort.setMaterial(list);
                        }
                        if (type == "fg")
                        {
                            flag = sort.setMaterial(list);
                        }
                        if (type == "fz")
                        {
                            flag = sort.setMaterial(list);
                        }
                        if (type == "tl")
                        {
                            flag = sort.setMaterial(list);
                        }
                        if (type == "fj")
                        {
                            flag = sort.setFran(list);
                        }
                        if (type == "kj")
                        {
                            flag = sort.setSize(list);
                        }
                        if (type == "yl")
                        {
                            flag = sort.setPressure(list);
                        }
                        if (type == "sj")
                        {
                            flag = sort.setDesign(list);
                        }
                        if (type == "lj")
                        {
                            flag = sort.setConn(list);
                        }
                        if (type == "jg")
                        {
                            flag = sort.setStruct(list);
                        }
                        if (type == "mf")
                        {
                            flag = sort.setSeal(list);
                        }
                    }
                    if (flag)
                    {
                        strRes = "OK";
                    }
                    else
                    {
                        strRes = "SET　ORDER FAILED";
                    }
                }
                WriteRes(strRes);
            }
            catch (Exception ex)
            {

            }

        }
        private void WriteRes(string res)
        {
            Response.Clear();
            Response.Write(res);
            Response.End();
        }
    }
}